<?php

namespace App\Repositories\Warehouse;

use Illuminate\Support\Facades\DB;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use App\Repositories\Warehouse\OrderGoodsInventoryCarryingDetailRepository;
use App\Models\Warehouse\OrderGoodsInventoryCarryingDetail;
use App\Validators\Warehouse\OrderGoodsInventoryCarryingDetailValidator;

/**
 * Class OrderGoodsInventoryCarryingDetailRepositoryEloquent.
 *
 * @package namespace App\Repositories\Warehouse;
 */
class OrderGoodsInventoryCarryingDetailRepositoryEloquent extends BaseRepository implements OrderGoodsInventoryCarryingDetailRepository
{
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return OrderGoodsInventoryCarryingDetail::class;
    }

    /**
     *   创建订单商品库存占用明细数据
     */
    public function AddOrderGoodsInventoryCarryingDetail(array $AddData){
        return $this->model->insertGetId($AddData);
    }

    /**
    *   根据订单ID或者订单编号来查询该笔订单商品库存占用明细
     *  参数说明（二选一）
     *  order_id  订单ID
     *  order_sn  订单号
     */
    public function GetOrderGoodsInventoryCarryingDetail(array $Data){
        $where=array();
        if(empty($Data['order_id']) && empty($Data['order_sn'])){
            return false;
        }
        if(!empty($Data['order_id'])){
            $where[]=['order_id','=',$Data['order_id']];
        }
        if(!empty($Data['order_sn'])){
            $where[]=['order_sn','=',$Data['order_sn']];
        }
        $list=$this->model->where($where)->get()->toArray();
        if($list){
            return $list;
        }else{
            return false;
        }
    }


    /**
     *   根据订单ID或者订单编号来批量修改该笔订单商品库存占用明细的行为状态
     *  默认占用1    1-占用   2-释放
     *  参数说明（二选一）
     *  order_id  订单ID
     *  order_sn  订单号
     *
     *  action  行为状态
     */
    public function UpdateActionBatch(array $Data){
        $where=array();
        if(!empty($Data['order_id'])){
            $where[]=['order_id','=',$Data['order_id']];
        }
        if(!empty($Data['order_sn'])){
            $where[]=['order_sn','=',$Data['order_sn']];
        }
        $UpdateAction=$Data['action'];
        if(empty($UpdateAction)){
            $UpdateAction=1;
        }
        $UpdateId=$this->model->where($where)->update(["action"=>$UpdateAction]);
        if($UpdateId){
            return $UpdateId;
        }else{
            return false;
        }
    }


    /**
     * 更新行为状态   默认占用1    1-占用   2-释放
     */
    public function UpdateAction($ogicd_id,$action){
        $flag = false;
        $data=$this->find($ogicd_id);
        if(!$data){
            return $flag;
        }

        /// 状态相同不处理
        if( $data->action == $action ) {
            return true;
        }

        /// 更新行为状态
        $data_u = $this->update(["action"=>$action], $ogicd_id);
        if( $data_u ) {
            $data_u->action_old = $data->action;
            return $data_u;
        }

        return $flag;
    }


    /**
     *   根据订单ID或者订单编号来批量修改该笔订单商品库存占用明细的行为状态
     *
     *  参数说明（二选一）
     *  order_id  订单ID
     *  order_sn  订单号
     *
     *  o_status  状态  默认0（0-未提交，1-未占用，2-已占用，3-占用异常，4-占用失败，5-未释放，6-已释放，7-释放失败，8-释放异常）
     */
    public function UpdateO_statusBatch(array $Data){
        $where=array();
        if(!empty($Data['order_id'])){
            $where[]=['order_id','=',$Data['order_id']];
        }
        if(!empty($Data['order_sn'])){
            $where[]=['order_sn','=',$Data['order_sn']];
        }
        $UpdateO_status=$Data['o_status'];
        if(empty($UpdateO_status)){
            $UpdateO_status=0;
        }
        $UpdateId=$this->model->where($where)->update(["o_status"=>$UpdateO_status]);
        if($UpdateId){
            return $UpdateId;
        }else{
            return false;
        }
    }


    /**
     * 更新状态  默认0（0-未提交，1-未占用，2-已占用，3-占用异常，4-占用失败，5-未释放，6-已释放，7-释放失败，8-释放异常
     */
    public function UpdateO_status($ogicd_id,$O_status){
        $flag = false;
        $data=$this->find($ogicd_id);
        if(!$data){
            return $flag;
        }

        /// 状态相同不处理
        if( $data->o_status == $O_status ) {
            return true;
        }

        /// 更新状态
        $data_u = $this->update(["o_status"=>$O_status], $ogicd_id);
        if( $data_u ) {
            $data_u->o_status_old = $data->O_status;
            return $data_u;
        }

        return $flag;
    }

    /**
    *  根据行为状态和对应明细状态批量查询数据
     * 参数说明
     * $Action 行为状态  默认1
     * $Status  状态   默认0
     */
    public function GetActionAndStatusListGroup($Action=1,$Status=0){
        $where=[
            ['action','=',$Action],
            ['o_status','=',$Status]
        ];
        $list=$this->model->where($where)->get()->toArray();
        if($list){
            $ListGroup=array();
            foreach ($list as $key=>$val){
                $ListGroup[$val['order_sn']][]=$val;
            }
            return $ListGroup;
        }else{
            return false;
        }
    }

    /**
     * 根据订单ID和商品货号，来修改绑定金蝶发货通知单单号，以及实际占用数，更新时间
     * */
    public function UpdateOGICData($updata){
        $where=[
            ['order_id','=',$updata['order_id']],
            ['goods_sn','=',$updata['goods_sn']]
        ];
        $data=[
            'consignment_note_sn'=>$updata['consignment_note_sn'],
            'actual_occupancy_num'=>$updata['goods_number'],
            'update_time'=>time(),
            'o_status'=>2
        ];
        $upid=$this->model->where($where)->update($data);
        if($upid){
            return $upid;
        }else{
            return false;
        }
    }

    public function UpdataActionAndO_statusBatch(array $Data){
        if(!empty($Data['order_id'])){
            $where[]=['order_id','=',$Data['order_id']];
        }
        if(!empty($Data['order_sn'])){
            $where[]=['order_sn','=',$Data['order_sn']];
        }
        $UpdateAction=$Data['action'];
        if(empty($UpdateAction)){
            $UpdateAction=1;
        }
        $UpdateO_status=$Data['o_status'];
        if(empty($UpdateO_status)){
            $UpdateO_status=0;
        }
        $UpdateId=$this->model->where($where)->update(["action"=>$UpdateAction,"o_status"=>$UpdateO_status]);
        if($UpdateId){
            return $UpdateId;
        }else{
            return false;
        }
    }

    /**
     * Boot up the repository, pushing criteria
     */
    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }
    
}
